package com.foolishbird.sdk.utils;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentOnAttachListener;
import androidx.fragment.app.FragmentTransaction;

import com.foolishbird.sdk.params.bean.FloatMenu;
import com.foolishbird.sdk.ui.fragment.FloatActivityFragment;
import com.foolishbird.sdk.ui.fragment.FloatGameServerFragment;
import com.foolishbird.sdk.ui.fragment.FloatGiftFragment;
import com.foolishbird.sdk.ui.fragment.FloatGonlueFragment;
import com.foolishbird.sdk.ui.fragment.FloatKefuFragment;
import com.foolishbird.sdk.ui.fragment.FloatNoticeFragment;
import com.foolishbird.sdk.ui.fragment.FloatSettingFragment;
import com.foolishbird.sdk.ui.fragment.FloatWebFragment;
import com.foolishbird.sdk.ui.fragment.RedPackageMainFragment;
import com.foolishbird.sdk.ui.fragment.RedpackageQRCodeFragment;

import java.util.Stack;

public class MyFragmentManger {


    private FragmentManager myManager;
    private View resId;
    private Context myContext;
//    private Stack<Fragment> flowStack;

    private Fragment activityFragment,giftFragment,serverFragment,gonglueFragment,settingFragment,noticeFragment,kefuFragment;



    private MyFragmentManger() {
    }

    private volatile static MyFragmentManger instance;

    public static MyFragmentManger getInstance() {
        if (instance == null) {
            synchronized (MyFragmentManger.class) {
                if (instance == null) {
                    instance = new MyFragmentManger();
                }
            }
        }
        return instance;
    }

    public void init(Context context,FragmentManager manger, View resid){
        this.myManager = manger;

        this.resId = resid;
        this.myContext = context;
//        flowStack = new Stack<Fragment>();
    }

    public void goDefaultFragment(FloatMenu menu){
        if(menu.getType()==1){
            if("game_activity".equals(menu.getIdentifier())){
                if(activityFragment==null){
                    activityFragment = FloatActivityFragment.newInstance(null);
                }
                changeFragment(activityFragment);
            }else if ("game_card".equals(menu.getIdentifier())){
                if(giftFragment == null){
                    giftFragment = FloatGiftFragment.newInstance(null);
                }
                changeFragment(giftFragment);
            }else if ("game_server".equals(menu.getIdentifier())){
                if(serverFragment==null){
                    serverFragment = FloatGameServerFragment.newInstance(null);
                }
                changeFragment(serverFragment);
            }else if ("gaming_strategy".equals(menu.getIdentifier())){
                if(gonglueFragment==null){
                    gonglueFragment = FloatGameServerFragment.newInstance(null);
                }
                changeFragment(gonglueFragment);
            }else if ("personal_setting".equals(menu.getIdentifier())){
                if(settingFragment==null){
                    settingFragment = FloatSettingFragment.newInstance(null);
                }
                changeFragment(settingFragment);
            }else if ("game_notice".equals(menu.getIdentifier())){
                if(noticeFragment ==null){
                    noticeFragment = FloatNoticeFragment.newInstance(null);
                }
                changeFragment(noticeFragment);
            }else if ("customer".equals(menu.getIdentifier())){
                if(kefuFragment == null){
                    kefuFragment = FloatKefuFragment.newInstance(null);
                }
                changeFragment(kefuFragment);
            }else if ("cash_red".equals(menu.getIdentifier())){
                changeFragment(RedPackageMainFragment.newInstance(null));
            }else{
                Toast.makeText(myContext,"敬请期待",Toast.LENGTH_SHORT).show();
            }
        }else {
            Bundle bundle= new Bundle();
            bundle.putSerializable("menu",menu);
            changeFragment(FloatWebFragment.newInstance(bundle));
        }
//        switch (myId){
//            case 1:
////                if(activityFragment==null){
////                    activityFragment = FloatActivityFragment.newInstance(null);
////                }
////                changeFragment(activityFragment);
//                    changeFragment(RedPackageMainFragment.newInstance(null));
//                break;
//            case 2:
//                if(giftFragment == null){
//                    giftFragment = FloatGiftFragment.newInstance(null);
//                }
//                changeFragment(giftFragment);
//                break;
//            case 3:
//                if(serverFragment==null){
//                    serverFragment = FloatGameServerFragment.newInstance(null);
//                }
//                changeFragment(serverFragment);
////                flowStack.push(serverFragment);
//                break;
//            case 4:
//                if(gonglueFragment==null){
//                    gonglueFragment = FloatGonlueFragment.newInstance(null);
//                }
//                changeFragment(gonglueFragment);
//                break;
//            case 5:
//                if(settingFragment==null){
//                    settingFragment = FloatSettingFragment.newInstance(null);
//                }
//                changeFragment(settingFragment);
////                flowStack.push(settingFragment);
//                break;
//            case 6:
//                if(noticeFragment ==null){
//                    noticeFragment = FloatNoticeFragment.newInstance(null);
//                }
//                changeFragment(noticeFragment);
//
//                break;
//            case 7:
//                if(kefuFragment == null){
//                    kefuFragment = FloatKefuFragment.newInstance(null);
//                }
//                changeFragment(kefuFragment);
//                break;
//            case 8:
//                Toast.makeText(myContext,"敬请期待",Toast.LENGTH_SHORT).show();
//                break;
//            default:
//                Toast.makeText(myContext,"开发中",Toast.LENGTH_SHORT).show();
//                break;
//
//        }
    }

    private void changeFragment(Fragment fragment){
        resId.setVisibility(View.VISIBLE);
        if (fragment.isAdded()){
            FragmentTransaction ft = myManager.beginTransaction();
            ft.show(fragment);
            ft.commitAllowingStateLoss();
//            flowStack.push(noticeFragment);
        }else {
            FragmentTransaction fragmentTransaction = myManager.beginTransaction();
//        fragmentTransaction.replace(resId.getId(), fragment);//动态添加
            fragmentTransaction.add(resId.getId(), fragment);
//        if (flowStack.size() > 1) {
//            fragmentTransaction.addToBackStack(null);
//        }
//        fragmentTransaction.addToBackStack(fragment.getClass().getSimpleName());
            fragmentTransaction.commitAllowingStateLoss();//提交  提交  提交（很重要）
//            flowStack.push(noticeFragment);
        }
    }

    public void gotoFragment(Fragment fm){
        changeFragment(fm);
    }

//    public void goAndPopFragment(Fragment now,Fragment newfm){
//
//        changeFragment(fm);
//    }

//    public void goBack(){
//        if(flowStack.size()<=1){
//            flowStack.clear();
//            resId.setVisibility(View.GONE);
//        }else {
//            Fragment fragment = flowStack.pop();
//            FragmentTransaction ft = myManager.beginTransaction();
//            ft.remove(fragment);
//            ft.commit();
//
//
////
////            flowStack.pop();
////            Fragment fm = flowStack.pop();
////            changeFragment(fm);
//        }
//    }
//public void goBack(){
//        goBack(flowStack.peek());
//}
    public void goBack(Fragment fm){

////        fm.onDestroy();
//        WL.d("test","aaaaa"+myManager.getFragments().size()+".."+fm.getClass().getSimpleName()+".."+
//                myManager.getFragments().get(myManager.getFragments().size()-1).getClass().getSimpleName());
//        if(myManager.getFragments().size()<=1){
//            FragmentTransaction ft = myManager.beginTransaction();
//            ft.remove(fm);
//            ft.commit();
////            flowStack.clear();
//            resId.setVisibility(View.GONE);
//        }else {
//            myManager.getFragments().get(myManager.getFragments().size()-2).onHiddenChanged(false);
//            FragmentTransaction ft = myManager.beginTransaction();
//            ft.remove(fm);
//            ft.commit();
//            WL.d("test","aaaaa"+myManager.getFragments().size()+".."+fm.getClass().getSimpleName()+".."
//                   );
////            flowStack.pop();
//
//        }
        goBack(fm,false);

    }
    public void goBackOnly(Fragment fm){
        if(myManager.getFragments().size()<=1){
            FragmentTransaction ft = myManager.beginTransaction();
            ft.remove(fm);
            ft.commit();
//            flowStack.clear();
            resId.setVisibility(View.GONE);
        }else {
            FragmentTransaction ft = myManager.beginTransaction();
            ft.remove(fm);
            ft.commit();
            WL.d("test","aaaaa"+myManager.getFragments().size()+".."+fm.getClass().getSimpleName()+".."
            );
//            flowStack.pop();

        }
    }


    public void goBack(Fragment fm,boolean noRefresh){

////        fm.onDestroy();
        WL.d("test","aaaaa"+myManager.getFragments().size()+".."+fm.getClass().getSimpleName()+".."+
                myManager.getFragments().get(myManager.getFragments().size()-1).getClass().getSimpleName());
        if(myManager.getFragments().size()<=1){
            FragmentTransaction ft = myManager.beginTransaction();
            ft.remove(fm);
            ft.commit();
//            flowStack.clear();
            resId.setVisibility(View.GONE);
        }else {
            try{
                myManager.getFragments().get(myManager.getFragments().size()-2).onHiddenChanged(noRefresh);
            }catch (Exception e){
                e.printStackTrace();
            }
            FragmentTransaction ft = myManager.beginTransaction();
            ft.remove(fm);
            ft.commit();
            WL.d("test","aaaaa"+myManager.getFragments().size()+".."+fm.getClass().getSimpleName()+".."
            );
//            flowStack.pop();

        }

    }

}
